package whu.o2o;

import java.io.Serializable;
import java.util.ArrayList;

import whu.o2o.adapter.DiaryAdapter;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ParseXml;
import whu.o2o.vo.CommentVO;
import whu.o2o.vo.DiaryVO;
import whu.o2o.vo.ImageVO;
import whu.o2o.vo.PersonVO;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import whu.o2o.R;

public class DiaryListActivity extends DiaryBaseActivity {
	private ListView mListView;
	private DiaryAdapter mAdapter;
	private ArrayList<DiaryVO> mDiaryList;
	private int mDiaryType;
	private String mTitle;
	private String mUrl;
	private boolean flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_diary_list);
		getIntentData();
		initObject();
		initView();
		initListener();
		//initData();
	}

	@Override
	protected void parseData(final String xml) {
		mDiaryList.clear();

		mApplication.getThreadPool().execute(new Runnable() {

			@Override
			public void run() {
				ParseXml.getInstance().parseDiaryXml(xml, mDiaryList);
				mHttpListHandler.sendEmptyMessage(ConstantData.Data.PARSE_XML_MSG_CODE);
			}
		});

	}

	@Override
	protected void setAdapter() {
		if (mAdapter == null) {
			mAdapter = new DiaryAdapter(this, mDiaryList, mDiaryType);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();
		}

	}

	@SuppressWarnings("unchecked")
	private void getIntentData() {
		Intent intent = getIntent();
		if (intent != null) {
			mTitle = intent.getStringExtra("title");
			mUrl = intent.getStringExtra("url");
			flag = intent.getBooleanExtra("flag", false);

			Serializable serializable = intent.getSerializableExtra("diary");
			if (serializable != null) {
				mDiaryList = (ArrayList<DiaryVO>) serializable;
			}
		}
	}

	private void initObject() {
		if (mDiaryList == null) {
			mDiaryList = new ArrayList<DiaryVO>();
		}

		if (!TextUtils.isEmpty(mTitle)) {
			mDiaryType = ConstantData.Data.DEFAULT_DIARY_TYPE;
		} else {
			mDiaryType = ConstantData.Data.FAQ_DIARY_TYPE;
			findViewById(R.id.write).setVisibility(View.GONE);
		}
		if(flag)
			findViewById(R.id.write).setVisibility(View.GONE);
	}

	private void initView() {
		if (!TextUtils.isEmpty(mTitle)) {
			setTitle(mTitle);
		} else {
			setTitle("搜索结果");
		}

		mListView = (ListView) findViewById(R.id.listView);
	}

	private void initListener() {
		findViewById(R.id.write).setOnClickListener(this);
		backBtnClick();
	}

	private void initData() {
		if (!TextUtils.isEmpty(mUrl)) {
			requestData();
		} else {
			setAdapter();
		}
	}

	private void requestData() {
//		Params params = new Params();
//		params.setUrl(mUrl);
//		params.setHandler(mHttpHandler);
//		params.setRequestType(ConstantData.Data.GET_HTTP);
//		params.setMsgCode(ConstantData.Data.GET_HTTP_RESULT_MSG_CODE);
//		params.setActivity(this);
//		params.setShowLoad(true);
//		LoadDialog.showDailog(this);
//		mApplication.getThreadPool().execute(new CommonHttpConnect(params));
		
		
		
		
		String[] times = { "23分钟前", "50分钟前", "54分钟前" };
		String[] contents = { "【四川】初冬，赴一场色彩的盛宴（淡季游九寨黄龙攻略）", "【四川】初冬，赴一场色彩的盛宴（淡季游九寨黄龙攻略）", "【四川】初冬，赴一场色彩的盛宴（淡季游九寨黄龙攻略）" };
		int[] states = { 0, 0, 0 };

		String[] names = { "小青", "小白", "小红" };
		int[] userIds = { 322, 128, 224 };
		int[] ages = { 32, 18, 24 };
		int[] sexs = { 0, 1, 0 };
		boolean[] validateds = { false, true, true };

		for (int i = 0; i < contents.length; i++) {
			DiaryVO diaryVO = new DiaryVO();

			PersonVO personVO = new PersonVO();
			personVO.setId(userIds[i]);
			personVO.setName(names[i]);
			personVO.setSex(sexs[i]);
			personVO.setAge(ages[i]);
			personVO.setValidated(validateds[i]);

			ArrayList<ImageVO> images = new ArrayList<ImageVO>();
			images.add(new ImageVO());

			ArrayList<CommentVO> comments = new ArrayList<CommentVO>();
			CommentVO commentVO1 = new CommentVO();
			commentVO1.setType(0);
			ArrayList<PersonVO> goodList = new ArrayList<PersonVO>();
			for (int j = 0; j < names.length; j++) {
				PersonVO personVOGood = new PersonVO();
				personVOGood.setName(names[j]);
				goodList.add(personVOGood);
			}
			commentVO1.setGoodList(goodList);

			CommentVO commentVO2 = new CommentVO();
			commentVO2.setType(1);
			PersonVO personVOComment = new PersonVO();
			personVOComment.setName(names[i]);
			commentVO2.setUser(personVOComment);
			commentVO2.setContent(contents[i]);
			comments.add(commentVO1);
			comments.add(commentVO2);

			diaryVO.setUser(personVO);
			diaryVO.setContent(contents[i]);
			diaryVO.setTime(times[i]);
			diaryVO.setSendState(states[i]);
			diaryVO.setImages(images);
			diaryVO.setComments(comments);

			mDiaryList.add(diaryVO);
		}

		setAdapter();
	}
}
